package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.PicStyle;
import ex.com.jm.food.service.CompanyMenuService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 企业对菜单管理action层
 * @author zhaojw
 *
 */
@SuppressWarnings("serial")
public class CompanyMenuAction extends ActionSupport {
	
	private CompanyMenuService comMenuService;
	private Menu menu;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String order;
	private String sort;
	private Integer type;
	private Integer status;
	private String s_date;
	private String e_date;
	private String menuId;
	private String comId;
	private String menuName;
	private Integer subId;
	private Integer supId;
	private List<PicStyle> picStyles=new ArrayList<PicStyle>();

	public Integer getSubId() {
		return subId;
	}

	public void setSubId(Integer subId) {
		this.subId = subId;
	}

	public Integer getSupId() {
		return supId;
	}

	public void setSupId(Integer supId) {
		this.supId = supId;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getComId() {
		return comId;
	}

	public void setComId(String comId) {
		this.comId = comId;
	}

	public int getInitPage() {
		return initPage;
	}

	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}

	public int getPageRow() {
		return pageRow;
	}

	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getS_date() {
		return s_date;
	}

	public void setS_date(String s_date) {
		this.s_date = s_date;
	}

	public String getE_date() {
		return e_date;
	}

	public void setE_date(String e_date) {
		this.e_date = e_date;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public CompanyMenuService getComMenuService() {
		return comMenuService;
	}

	public void setComMenuService(CompanyMenuService comMenuService) {
		this.comMenuService = comMenuService;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public List<PicStyle> getPicStyles() {
		return picStyles;
	}

	public void setPicStyles(List<PicStyle> picStyles) {
		this.picStyles = picStyles;
	}

	public String menuList(){
		return "menuList";
	}
	
	public void getMenus() {
	 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 int offset=(initPage-1)*pageRow;
		 System.out.println(comId);
		 List<Menu> menus=this.getComMenuService().selectMenu(offset, pageRow, menuName,  type,
					 status,comId,  s_date,  e_date, order, sort);
		 JSONArray jsonmenu=JSONArray.fromObject(menus);
		 totalCount=this.getComMenuService().menuConut(comId, menuName, type, status, s_date, e_date);
				 
		 JSONObject jo=JsonUtils.buildJson(jsonmenu, initPage, pageRow, totalCount);
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
			out.flush();
			out.close();
	}
	
	public String  editMenu(){
		picStyles=this.getComMenuService().getPicStyles();
		if (menuId != null) { 
			menu = this.getComMenuService().editMenu(menuId);
		}
		return "edit";

		
	}
	public String saveMenu(){
		if(menu.getMenuName()==null||menu.getMenuName().equals("")){
			addFieldError("menu.menuName", "菜单名称不能为空");
			return "edit";
		}
		if (menu.getMenuPrice()==null||menu.getMenuPrice().equals("")) {
			addFieldError("menu.menuPrice", "菜单价格不能为空");
			return "edit";
		}
		if(menu.getMenuMaterial()==null||menu.getMenuMaterial().equals("")){
			addFieldError("menu.menuMaterial", "菜单原料不能为空");
			return "edit";
		}
		if(menu.getMenuPractice()==null||menu.getMenuPractice().equals("")){
			addFieldError("menu.menuPractice", "菜品做法不能为空");
			return "edit";
		}
		menu.setStatus(0);//只要是上传的菜品或者更新的菜品，其状态均为待审核
		menu.setMenuType(0);//只要是上传的菜品或者更新的菜品，其状态均为新菜
		menu.setComId((Company)ActionContext.getContext().getSession().get("company"));
		if(menu.getMenuId()!=null&&!menu.getMenuId().equals("")){
			if(menu.getMenuInDate()==null){
				menu.setMenuInDate("--:--:--");
			}
			if(menu.getMenuNowPrice()==null){
				menu.setMenuNowPrice(menu.getMenuPrice());
			}
			this.getComMenuService().updateMenu(menu);
			return "list";
		}
		menu.setMenuDate(DateUtils.getCurrentDate());
		menu.setMenuInDate("--:--:--");
		menu.setMenuNowPrice(menu.getMenuPrice());
		this.getComMenuService().saveMenu(menu);
		return "list";
	}
	
	public String deleteMenu(){
		this.getComMenuService().deleteMenu(menuId);
		return "list";
	}
}
	
